package com.learning.servlet2x.cookie;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: LoginOutCookieServlet
 * Description: 永久登录-注销的servlet
 * Date: 2018/6/24 13:03 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class LoginOutCookieServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;


    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);


    }

    /**
     * post方法访问，处理注销，删除Cookie
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String action = request.getParameter("action");

        if ("logout".equals(action)) {

            // 删除Cookie中的帐号
            Cookie accountCookie = new Cookie("account", "");
            accountCookie.setMaxAge(0);

            // 删除Cookie中的加密结果
            Cookie ssidCookie = new Cookie("ssid", "");
            ssidCookie.setMaxAge(0);

            response.addCookie(accountCookie);
            response.addCookie(ssidCookie);

            // 重定向到校验servlet
            response.sendRedirect(this.getServletContext().getContextPath() + "/checkCookieServlet");
            return;
        }

    }

}
